import { ipcMain, dialog, BrowserWindow, screen } from "electron";
import Server from "../server";
import { winURL } from "../config/StaticPath";
import { updater } from "./HotUpdater";

export default {
  Mainfunc(mainWindow: BrowserWindow, IsUseSysTitle: Boolean) {
    ipcMain.handle("IsUseSysTitle", async () => {
      return IsUseSysTitle;
    });
    ipcMain.handle("windows-mini", () => {
      mainWindow.minimize();
    });
    ipcMain.handle("window-max", async () => {
      if (mainWindow.isMaximized()) {
        mainWindow.restore();
        return { status: false };
      } else {
        mainWindow.maximize();
        return { status: true };
      }
    });
    ipcMain.handle("window-close", () => {
      mainWindow.close();
    });
    ipcMain.handle("open-messagebox", async (event, arg) => {
      const res = await dialog.showMessageBox(mainWindow, {
        type: arg.type || "info",
        title: arg.title || "",
        buttons: arg.buttons || [],
        message: arg.message || "",
        noLink: arg.noLink || true,
      });
      return res;
    });
    ipcMain.handle("open-errorbox", (event, arg) => {
      dialog.showErrorBox(arg.title, arg.message);
    });
    ipcMain.handle("statr-server", async () => {
      try {
        const serveStatus = await Server.StatrServer();
        console.log(serveStatus);
        return serveStatus;
      } catch (error) {
        dialog.showErrorBox("错误", error);
      }
    });
    ipcMain.handle("stop-server", async (event, arg) => {
      try {
        const serveStatus = await Server.StopServer();
        return serveStatus;
      } catch (error) {
        dialog.showErrorBox("错误", error);
      }
    });
    ipcMain.handle("hot-update", (event, arg) => {
      updater(mainWindow);
    });
    ipcMain.handle("open-win", (event, arg) => {
      const ChildWin = new BrowserWindow({
        height: 595,
        useContentSize: true,
        width: 842,
        autoHideMenuBar: true,
        minWidth: 842,
        show: false,
        webPreferences: {
          nodeIntegration: true,
          contextIsolation: false,
          webSecurity: false,
          // 如果是开发模式可以使用devTools
          devTools: process.env.NODE_ENV === "development",
          // devTools: true,
          // 在macos中启用橡皮动画
          scrollBounce: process.platform === "darwin",
        },
      });
      ChildWin.loadURL(winURL + `#${arg.url}`);
      ChildWin.webContents.once("dom-ready", () => {
        ChildWin.show();
        ChildWin.webContents.send("send-data", arg.sendData);
        if (arg.IsPay) {
          // 检查支付时候自动关闭小窗口
          const testUrl = setInterval(() => {
            const Url = ChildWin.webContents.getURL();
            if (Url.includes(arg.PayUrl)) {
              ChildWin.close();
            }
          }, 1200);
          ChildWin.on("close", () => {
            clearInterval(testUrl);
          });
        }
      });
    });
    ipcMain.handle("set-size", (event, arg) => {
      const size = arg.split("*").map((m) => +m);
      mainWindow.setSize(size[0], size[1]);
      mainWindow.setContentSize(size[0], size[1]);
      mainWindow.center();
    });
    ipcMain.handle("get-size", () => {
      const params = {
        size: mainWindow.getSize(),
        fullScreen: mainWindow.isFullScreen(),
      };
      mainWindow.webContents.send("win-size", params);
    });
    ipcMain.handle("set-max", async (e, arg) => {
      if (arg) {
        const { width, height } = screen.getPrimaryDisplay().size;
        mainWindow.setContentSize(width, height);
        mainWindow.setFullScreen(true);
      } else mainWindow.setFullScreen(false);
    });
  },
};
